) { $option = array_merge( $this->get_option(), [ 'token' => $token, ] ); return update_option( $this->opt_in_status->get_option_name(), $option ); } /** * Determines if the current site is registered on the telemetry server. * * @since 1.0.0 * * @return bool */ public function is_registered() { // Check if the site is registered by checking if the token is set. $option = $this->get_option(); return ! empty( $option['token'] ); } /** * Sends data to the telemetry server. * * @since 1.0.0 * * @return bool */ public function send_data() { if ( ! $this->is_registered() ) { return false; } if ( ! $this->opt_in_status->is_active() ) { return false; } $response = $this->send( $this->get_send_data_args(), $this->get_send_data_url() ); return isset( $response['status'] ); } /** * Gets the stored auth token for the current site. * * @since 1.0.0 * * @return string */ public function get_token() { $option = $this->get_option(); return $option['token'] ?? ''; } /** * Sends requests to the telemetry server and parses the response. * * @since 1.0.0 * * @param array $data The array of data to send. * @param string $url The url of the telemetry server. * @param bool $blocking Whether the request should be blocking or not. * @param float $timeout The timeout for the request, `0` for no timeout. * * @return array|null */ public function send( array $data, string $url, bool $blocking = true, float $timeout = 5.0 ) { if ( ! $this->opt_in_status->is_active() ) { return null; } $response = $this->request( $url, $data, $blocking, $timeout ); if ( is_wp_error( $response ) ) { return null; } $response = $this->parse_response( $response ); if ( empty( $response['status'] ) ) { return null; } return $response; } /** * Actually sends the request to the telemetry server. * * @since 1.0.0 * * @param string $url The url of the telemetry server. * @param array $data The data to send. * @param bool $blocking Whether the request should be blocking or not. * @param float $timeout The timeout for the request, `0` for no timeout. * * @return array|\WP_Error */ protected function request( string $url, array $data, bool $blocking = true, float $timeout = 5.0 ) { return wp_remote_post( $url, [ 'blocking' => $blocking, 'timeout' => $timeout, 'body' => $data, ] ); } /** * Parses responses from wp_remote_requests. * * @since 1.0.0 * * @param array $response The response from a request. * * @return array|null */ protected function parse_response( array $response ) { $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); // If status is false, return null. if ( false === ( $data['status'] ?? false ) ) { return null; } return $data; } /** * Gets the registered site url. * * @since 1.0.0 * * @return string */ protected function get_register_site_url() { /** * Filters the registered site url. * * @since 1.0.0 * * @param string $site_url */ return apply_filters( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'register_site_url', Config::get_server_url() . '/register-site' ); } /** * Gets the uninstall url. * * @since 1.0.0 * * @return string */ protected function get_uninstall_url() { /** * Filters the uninstall url. * * @since 1.0.0 * * @param string $uninstall_url */ return apply_filters( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'uninstall_url', Config::get_server_url() . '/uninstall' ); } /** * Gets the registered site data. * * @since 1.0.0 * * @return array */ protected function get_register_site_data() { /** * Filters the register site data. * * @since 1.0.0 * * @param array $register_site_data */ return apply_filters( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'register_site_data', [ 'telemetry' => wp_json_encode( $this->provider->get_data() ), 'stellar_slugs' => wp_json_encode( $this->opt_in_status->get_opted_in_plugins() ), ] ); } /** * Gets the current user's details. * * @since 1.0.0 * @since 2.0.0 - Add support for passing stellar_slug directly. * * @param string $stellar_slug The plugin slug to pass to the server when registering a site user. * @param string $opt_in_text The opt-in text displayed to the user when they agreed to share their data. * * @return array */ protected function get_user_details( string $stellar_slug = '', string $opt_in_text = '' ) { if ( '' == $stellar_slug ) { $stellar_slug = Config::get_stellar_slug(); } $user = wp_get_current_user(); $template = Config::get_container()->get( Opt_In_Template::class ); $args = [ 'name' => $user->display_name, 'email' => $user->user_email, 'plugin_slug' => $stellar_slug, 'opt_in_text' => $opt_in_text, ]; /** * Filters the site user details. * * @since 1.0.0 * @since 2.0.0 - Add parameter for the current stellar_slug. * * @param array $site_user_details The details passed to the telemetry server for registering a new site user. * @param string $stellar_slug The current stellar slug passed with the user. */ $user_info = apply_filters( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'register_site_user_details', $args, $stellar_slug ); return [ 'user' => wp_json_encode( $user_info ) ]; } /** * Gets the telemetry option. * * @since 1.0.0 * * @return array */ protected function get_option() { return get_option( $this->opt_in_status->get_option_name(), [] ); } /** * Gets the args for sending data to the telemetry server. * * @since 1.0.0 * @since 2.3.0 - Updated to include the opted in user with the telemetry json. * * @return array */ protected function get_send_data_args() { $opt_in_user = get_option( Status::OPTION_NAME_USER_INFO, [] ); $telemetry = $this->provider->get_data(); if ( count( $opt_in_user ) > 0 ) { $telemetry['opt_in_user'] = json_decode( $opt_in_user['user'], true ); } $args = [ 'token' => $this->get_token(), 'telemetry' => wp_json_encode( $telemetry ), 'stellar_slugs' => wp_json_encode( $this->opt_in_status->get_opted_in_plugins() ), ]; return apply_filters( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'send_data_args', $args ); } /** * Gets the URL for sending data to the telemetry server. * * @since 1.0.0 * * @return string */ protected function get_send_data_url() { /** * Filter the url for sending data to the telemetry server. * * @since 1.0.0 * * @param string $data_url */ return apply_filters( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'send_data_url', Config::get_server_url() . '/telemetry' ); } }